#region Using directives

/*
 * Copyright (c) 2005-2007 Alan Dean.
 * All rights reserved.
 */

using System;
using System.Collections.Generic;
using System.Globalization;

#endregion

namespace ObjectThinking
{
    public class GenericSelf : Dictionary<Uri, Object>, ISelf
    {
        // public constructors

        public GenericSelf()
            : this(new List<IRule>())
        { }

        public GenericSelf(ICollection<IRule> rules)
        {
            _rules = rules;
        }

        // public indexer

        new public Object this[Uri key]
        {
            get
            {
                object value = null;
                
                if (base.ContainsKey(key)) value = base[key];

                return value;
            }
            set
            {
                base[key] = value;
            }
        }

        // public properties (ISelf)

        public ICollection<IRule> Rules
        {
            get { return _rules; }
        }
        private ICollection<IRule> _rules;

        // public methods (ISelf)

        public bool Validate()
        {
            bool valid = true;

            foreach (IRule rule in Rules)
            {
                if (!rule.Validate(this)) valid = false;
            }

            foreach (KeyValuePair<Uri, Object> pair in this)
            {
                ISelf obj = (pair.Value as ISelf);
                if ((obj != null) && (!obj.Validate())) valid = false;
            }

            return valid;
        }
    }
}